//
//  BBProjectionConfigView.m
//  BBTouch
//
//  Created by ben smith on 6/14/08.
//  This file is part of BBTouch.
//
//  BBTouch is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.

//  BBTouch is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Lesser General Public License for more details.

//  You should have received a copy of the GNU Lesser General Public License
//  along with BBTouch.  If not, see <http://www.gnu.org/licenses/>.
// 
//  Copyright 2008 Ben Britten Smith ben@benbritten.com .
//
//

#import "BBProjectionConfigView.h"
#import "BBConfigurationController.h"
#import "BBBlobEvent.h"


@implementation BBProjectionConfigView
-(void)awakeFromNib
{
	[super awakeFromNib];
	[[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:@"BBROIStringValue" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
	[self setNeedsDisplay:YES];
}

- (void)drawRect:(NSRect)rect 
{
	[[NSColor blackColor] set];
	[NSBezierPath strokeRect:[self bounds]];
	
	roiRect = [[BBConfigurationController sharedConfigurationController] regionOfInterest];
	// what i want here is to scale the ROI so it fills the entire bounds (while keeping the aspect ratio)
	// scale, so find the scale diff between the roi and my bounds,
	// then apply that to the raw size
	if (roiRect.size.width > roiRect.size.height) {
		scale.x = [self bounds].size.width/roiRect.size.width;
		scale.y = scale.x;
	} else {
		scale.x = [self bounds].size.height/roiRect.size.height;
		scale.y = scale.x;			
	}
	[self scaleUnitSquareToSize:NSMakeSize(scale.x, scale.y)];
	
	// move the everything to the center
	roiRect.origin.x += NSMidX([self bounds]) - NSMidX(roiRect);
	roiRect.origin.y += NSMidY([self bounds]) - NSMidY(roiRect);
	[[NSColor lightGrayColor] set];
	NSRectFill(roiRect);	
	[super drawRect:rect];
}

-(void)drawBlobEvent:(BBBlobEvent*)theBlobEvent
{
	float radius = [theBlobEvent radius];
	NSPoint bLoc = [theBlobEvent screenLocation];
	// blob location is in a normalizes coordinate, so from 0.0 - 1.0
	// need to scale it out to match my bounds
	bLoc.x *= roiRect.size.width;
	bLoc.y *= roiRect.size.height;
	bLoc.x += roiRect.origin.x;
	bLoc.y += roiRect.origin.y;
	NSRect blobRect = NSMakeRect(bLoc.x - radius, bLoc.y - radius, radius * 2.0, radius * 2.0);
	// make a pretty oval in a rectangle. oooh!
	[[NSColor greenColor] set];
	[NSBezierPath strokeRect:blobRect];
	[[NSColor purpleColor] set];
	[[NSBezierPath bezierPathWithOvalInRect:blobRect] stroke];
}



@end
